package com.common.app.data.repository

import com.common.app.data.api.ApiConstant
import com.common.app.data.api.ApiConstant.getBaseUrl
import com.common.app.data.bean.UpFileModel
import com.common.app.data.bean.VersionResBean
import com.common.app.data.bean.user.*
import com.common.app.utls.Utils
import com.common.base.app.extras.no
import com.common.base.app.extras.yes
import com.common.base.data.CommonBean
import com.common.base.data.ListCommon
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import rxhttp.toAwait
import rxhttp.wrapper.param.RxHttp
import rxhttp.wrapper.param.RxHttpJsonParam
import java.io.File

/**
 * 作者：sosou
 * <p>
 * 版本：1.0
 * <p>
 * 创建日期：2022/8/20
 * <p>
 * 描述：
 * <p>
 * 修订历史：
 */
object UserApi {

    val time = Utils.getTimestamp()

    //头像上传
    private val USER_UPLOAD_HEAD_IMAGE =
        ApiConstant.getBaseUrl() + "/api/v1/smart-ring/file/upload"

    //图片上传接口
    private val USER_UPLOAD_IMAGE = ApiConstant.getBaseUrl() + "live-passport-api/upload/image"

    //用户登录
    private val USER_LOGIN = ApiConstant.getBaseUrl() + "api/v1/smart-ring/sign-in"

    //APP更新
    private val USER_VERSION_LAST = ApiConstant.getBaseUrl() + "live-passport-api/version/last"

    //获取用户信息
    private val USER_GET_INFO = ApiConstant.getBaseUrl() + "api/v1/smart-ring/profile"

    //修改用户数据
    private val MODIFY_USER_INFO = ApiConstant.getBaseUrl() + "api/v1/smart-ring/set-personal-profile"

    //获取个人指标对象
    private val USER_GET_GOAL_INFO = ApiConstant.getBaseUrl() + "api/v1/ocell-ring/metrics/personal/object"

    //设置个人指标对象
    private val USER_SET_GOAL_INFO = ApiConstant.getBaseUrl() + "api/v1/ocell-ring/metrics/personal"
    //提交反馈
    private val SUBMIT_FEEDBACK = getBaseUrl() + "api/v1/ocell-ring/xxx"
    //获取提交历史
    private val GET_SUBMIT_LIST = getBaseUrl() + "api/v1/ocell-ring/xxx"

    //头像上传
    suspend fun uploadHeadImage(file: File, type:String = "other") = withContext(Dispatchers.IO) {
        RxHttp.postForm(USER_UPLOAD_HEAD_IMAGE)
            .add("type", type)
            .addFile("file", file)
            .toAwait<CommonBean<UpFileModel>>()
            .await()
    }

    //登录
    suspend fun login(
        account: String,
        pwd: String,
        authType: Int,
    ) =
        withContext(Dispatchers.IO) {
            RxHttp.postJson(USER_LOGIN)
                .add("Account", account)
                .add("Code", pwd)
                .add("AuthType", authType)
                .toAwait<CommonBean<LoginBean>>()
                .await()
        }

    //App更新
    suspend fun getVersionLast() =
        withContext(Dispatchers.IO) {
            RxHttp.get(USER_VERSION_LAST)
                .add("platformCategory", 1)
                .toAwait<CommonBean<VersionResBean>>()
                .await()
        }


    //获取用户信息
    suspend fun getUserInfo() = withContext(Dispatchers.IO) {
        RxHttp.get(USER_GET_INFO)
            .toAwait<CommonBean<UserBean>>()
            .await()
    }


    //修改用户数据
    suspend fun modifyUsrInfo(
        imageId: Long = 0,
        nickname: String?,
        birthday: String?,
        height: Int = 0,
        weight: Int = 0,
        sex: Int = -1,//0：女 1：男 -1：保密
        description: String?,
    ) =
        withContext(Dispatchers.IO) {
            val mRq: RxHttpJsonParam = RxHttp.postJson(MODIFY_USER_INFO)
            (imageId > 0).yes {
                mRq.add("avatar", imageId)
            }
            nickname.isNullOrEmpty().no {
                mRq.add("nickname", nickname)
            }
            birthday.isNullOrEmpty().no {
                mRq.add("birthday", birthday)
            }
            if(description != null){
                mRq.add("description", description)
            }
            (height > 0).yes {
                mRq.add("height", height)
            }
            (weight > 0).yes {
                mRq.add("weight", weight)
            }
            (sex > -2).yes {
                mRq.add("sex", sex)
            }

            mRq.toAwait<CommonBean<Any>>().await()
        }


    //获取用户目标信息
    suspend fun getUserGoalInfo() = withContext(Dispatchers.IO) {
        RxHttp.postJson(USER_GET_GOAL_INFO)
            .toAwait<CommonBean<UserGoalBean>>()
            .await()
    }

    //设置用户目标信息
    suspend fun setUserGoalInfo(metricType:Int,mValue:Int) = withContext(Dispatchers.IO) {

        val mRq: RxHttpJsonParam = RxHttp.postJson(USER_SET_GOAL_INFO)
        RxHttp.postJson(USER_SET_GOAL_INFO)

        when(metricType){
            101->{// 睡眠时长目标
                mRq.add("metric_type", "sleep_duration_target")
                mRq.add("target_value", mValue)
            }
            102->{//步数目标
                mRq.add("metric_type", "steps_target")
                mRq.add("target_value", mValue)
            }
            103->{// 卡路里目标
                mRq.add("metric_type", "calories_target")
                mRq.add("target_value", mValue)
            }
            104->{ // 活动时长目标
                mRq.add("metric_type", "activity_duration_target")
                mRq.add("target_value", mValue)
            }
        }
        mRq.toAwait<CommonBean<Any>>().await()
    }

    suspend fun requestSubmitFeedback() =
        withContext(Dispatchers.IO) {
            RxHttp.postJson(SUBMIT_FEEDBACK)
                .toAwait<CommonBean<Any>>()
                .await()
        }


    suspend fun requestHistoryList() =
        withContext(Dispatchers.IO) {
            RxHttp.postJson(GET_SUBMIT_LIST)
                .toAwait<ListCommon<UserFeedbackHistoryModel>>()
                .await()
        }
}